<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>GitCode 問題反饋插件測試</title>
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            line-height: 1.6;
            margin: 0;
            padding: 20px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
        }
        .container {
            max-width: 900px;
            margin: 0 auto;
            background: white;
            padding: 40px;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
            font-size: 2.5em;
        }
        .test-section {
            margin: 30px 0;
            padding: 20px;
            border: 2px solid #e0e0e0;
            border-radius: 10px;
            background: #f9f9f9;
        }
        .test-section h2 {
            color: #ff6b6b;
            margin-top: 0;
        }
        .test-button {
            background: linear-gradient(45deg, #ff6b6b, #ee5a24);
            color: white;
            border: none;
            padding: 15px 30px;
            border-radius: 25px;
            cursor: pointer;
            font-size: 16px;
            font-weight: bold;
            transition: all 0.3s ease;
            box-shadow: 0 4px 15px rgba(255, 107, 107, 0.3);
            margin: 10px;
        }
        .test-button:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(255, 107, 107, 0.4);
        }
        .test-button:active {
            transform: translateY(0);
        }
        .result {
            margin-top: 15px;
            padding: 15px;
            border-radius: 8px;
            font-family: 'Courier New', monospace;
            white-space: pre-wrap;
        }
        .success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        .info {
            background: #d1ecf1;
            color: #0c5460;
            border: 1px solid #bee5eb;
        }
        .code-block {
            background: #2d3748;
            color: #e2e8f0;
            padding: 20px;
            border-radius: 8px;
            margin: 15px 0;
            overflow-x: auto;
        }
        .highlight {
            background: #fff3cd;
            padding: 2px 4px;
            border-radius: 3px;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🔧 GitCode 問題反饋插件測試</h1>
        
        <div class="test-section">
            <h2>📋 測試說明</h2>
            <p>這個頁面用於測試 GitCode 問題反饋插件的核心功能，包括：</p>
            <ul>
                <li>✅ 插件加載和初始化</li>
                <li>✅ 按鈕創建和樣式</li>
                <li>✅ 文本選擇功能</li>
                <li>✅ URL 生成和跳轉</li>
                <li>✅ 錯誤處理機制</li>
            </ul>
        </div>

        <div class="test-section">
            <h2>🧪 功能測試</h2>
            <button class="test-button" onclick="testPluginLoad()">測試插件加載</button>
            <button class="test-button" onclick="testButtonCreation()">測試按鈕創建</button>
            <button class="test-button" onclick="testTextSelection()">測試文本選擇</button>
            <button class="test-button" onclick="testURLGeneration()">測試 URL 生成</button>
            <button class="test-button" onclick="testFullWorkflow()">測試完整流程</button>
            <div id="testResult" class="result" style="display: none;"></div>
        </div>

        <div class="test-section">
            <h2>📝 測試內容</h2>
            <p>請選擇下面的文本來測試文本選擇功能：</p>
            <div class="code-block">
// 這是一段示例代碼
function testFunction() {
    console.log("Hello, GitCode!");
    return "測試成功";
}

// 選擇這段文本，然後點擊"測試文本選擇"按鈕
const result = testFunction();
            </div>
        </div>

        <div class="test-section">
            <h2>⚙️ 配置信息</h2>
            <div class="code-block">
{
    "repo": "nutpi/gitbook-plugin-gitcode-issue-feedback",
    "label": "報告問題",
    "private": false
}
            </div>
        </div>
    </div>

    <!-- 加載插件 -->
    <script src="plugin.js"></script>
    
    <script>
        // 測試函數
        function showResult(message, type = 'info') {
            const resultDiv = document.getElementById('testResult');
            resultDiv.textContent = message;
            resultDiv.className = `result ${type}`;
            resultDiv.style.display = 'block';
        }

        function testPluginLoad() {
            try {
                if (typeof window.require === 'function') {
                    showResult('✅ 插件加載成功！\nwindow.require 函數可用', 'success');
                } else {
                    showResult('✅ 插件加載成功！\n使用非 GitBook 模式', 'success');
                }
            } catch (error) {
                showResult(`❌ 插件加載錯誤：\n${error.message}`, 'error');
            }
        }

        function testButtonCreation() {
            try {
                const button = document.querySelector('.gitbook-plugin-gitcode-issue-feedback');
                if (button) {
                    showResult('✅ 按鈕創建成功！\n按鈕已添加到頁面', 'success');
                } else {
                    showResult('⏳ 按鈕尚未創建\n等待插件初始化...', 'info');
                    // 等待一下再檢查
                    setTimeout(() => {
                        const button = document.querySelector('.gitbook-plugin-gitcode-issue-feedback');
                        if (button) {
                            showResult('✅ 按鈕創建成功！\n按鈕已添加到頁面', 'success');
                        } else {
                            showResult('❌ 按鈕創建失敗！\n未找到反饋按鈕', 'error');
                        }
                    }, 2000);
                }
            } catch (error) {
                showResult(`❌ 按鈕創建錯誤：\n${error.message}`, 'error');
            }
        }

        function testTextSelection() {
            try {
                const selection = window.getSelection();
                const selectedText = selection ? selection.toString() : '';
                
                if (selectedText.trim()) {
                    showResult(`✅ 文本選擇成功！\n選中的文本：\n"${selectedText}"`, 'success');
                } else {
                    showResult('ℹ️ 請先選擇一些文本\n然後再次點擊此按鈕', 'info');
                }
            } catch (error) {
                showResult(`❌ 文本選擇錯誤：\n${error.message}`, 'error');
            }
        }

        function testURLGeneration() {
            try {
                const config = {
                    repo: 'nutpi/gitbook-plugin-gitcode-issue-feedback',
                    label: '報告問題',
                    private: false
                };
                
                const baseUrl = `https://gitcode.com/${config.repo}/issues/new`;
                const params = new URLSearchParams();
                params.append('labels', config.label);
                params.append('title', '測試問題標題');
                params.append('body', '這是一個測試問題的內容');
                
                const fullUrl = `${baseUrl}?${params.toString()}`;
                
                showResult(`✅ URL 生成成功！\n生成的 URL：\n${fullUrl}`, 'success');
            } catch (error) {
                showResult(`❌ URL 生成錯誤：\n${error.message}`, 'error');
            }
        }

        function testFullWorkflow() {
            try {
                // 測試 BugReporter 類
                if (typeof BugReporter !== 'undefined') {
                    const reporter = new BugReporter('https://gitcode.com/nutpi/gitbook-plugin-gitcode-issue-feedback/issues/new');
                    reporter.setTitle('測試問題標題');
                    reporter.setBody('這是一個測試問題的內容');
                    
                    showResult('✅ 完整流程測試成功！\n所有功能都正常工作', 'success');
                } else {
                    showResult('❌ BugReporter 類未定義\n插件可能未正確加載', 'error');
                }
            } catch (error) {
                showResult(`❌ 完整流程測試錯誤：\n${error.message}`, 'error');
            }
        }

        // 頁面加載完成後的初始化
        document.addEventListener('DOMContentLoaded', function() {
            console.log('🚀 測試頁面已加載');
            console.log('📋 請按照頁面說明進行測試');
        });
    </script>
</body>
</html>
